import re
import matplotlib.pyplot as plt


# 定义读取文件的函数
def read_training_data(file_path):
    epochs = []
    f_scores = []

    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            # 使用正则表达式找到Epoch和f_score的值
            epoch_match = re.search(r'Epoch (\d+)/500', line)
            f_score_match = re.search(r'f_score=([0-9.]+)', line)

            if epoch_match:
                epoch = int(epoch_match.group(1))
                epochs.append(epoch)

                # 读取下一行中的第一个f_score值
                f_score_line = next(file, None)
                if f_score_line:
                    f_score_match = re.search(r'f_score=([0-9.]+)', f_score_line)
                    if f_score_match:
                        f_score = float(f_score_match.group(1))
                        f_scores.append(f_score)
                    else:
                        # 如果没有找到f_score，添加一个默认值
                        f_scores.append(0.0)
                else:
                    # 如果没有找到f_score，添加一个默认值
                    f_scores.append(0.0)

    return epochs, f_scores


# 定义绘制图形的函数
def plot_training_curve(epochs, f_scores):
    plt.plot(epochs, f_scores, marker='o')
    plt.xlabel('Epoch')
    plt.ylabel('f_score')
    plt.title('Training Progress')
    plt.grid(True)
    plt.show()



# 主程序
file_path = 'E:/postgraduate/大论文/F1-score.txt'  # 替换为你的文件路径
epochs, f_scores = read_training_data(file_path)
plot_training_curve(epochs, f_scores)
